-- TravellerTent
-- Created by chengb Aug/4/2016
-- 旅者帐篷

-- 初始化
function initTravellerTent(self)
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local elementDbase = DungeonM.queryElement(self.gridData.element, "dbase");

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            local function okFunc()
                -- 检查消耗
                local cost = FormulaM.invoke("CALC_TRAVELLER_TENT_COST");
                if cost[1] == 1 then
                    local classId = cost[2];
                    local amount  = cost[3];
                    if ItemM.getAmount(ME.user, classId) < amount then
                        local msg = string.format(getLocStr("something_not_enough"), ItemM.query(classId, "name"));
                        alert(msg);
                        return;
                    end
                elseif cost[2] == 2 then
                    local attrib = cost[2];
                    local amount = cost[3];
                    if ME.user:queryAttrib(attrib) < amount then
                        local msg = string.format(getLocStr("something_not_enough"), FieldsM.getFieldName(attrib));
                        alert(msg);
                        return;
                    end
                end

                -- 检查hp和mp是否都满了

                DungeonActionM.go("traveller_tent", self.gridData:getPos());
            end

            AudioM.playFx("ui_open1");
            self:openBuildingView2(okFunc);
        end
    end

    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
end

-- 旅者帐篷事件的回调
function onTravellerTentEvent(self, hp, mp)
    -- 播放光效
    playPaladinEffect(self.node);

    local bonusList = {};
    if hp > 0 then
        table.insert(bonusList, { 2, "hp", hp, });
    end

    if mp > 0 then
        table.insert(bonusList, { 2, "mp", mp });
    end

    if #bonusList > 0 then
        local function callback()
            self:playBonusListEffect(bonusList, false)
        end
        performWithDelay(self, callback, 3)
    end
end
